import React from 'react';
import { Menu } from 'antd';
import type { MenuProps } from 'antd';
import {
    AppstoreOutlined,
    DatabaseOutlined,
    SettingOutlined,
    ScanOutlined,
} from '@ant-design/icons';
import { useNavigate, useLocation } from 'react-router-dom';

const Sidebar: React.FC = () => {
    const navigate = useNavigate();
    const location = useLocation();

    // 判断当前选中的菜单项
    let selectedKey = '';
    let openKeys: string[] = [];

    if (location.pathname.startsWith('/unit') || location.pathname === '/units') {
        selectedKey = 'workbench';
    } else if (location.pathname.startsWith('/metrics')) {
        selectedKey = 'metrics';
    } else if (location.pathname.startsWith('/sa/scan-management')) {
        selectedKey = 'sa-scan-management';
        openKeys = ['admin'];
    }

    const menuItems: MenuProps['items'] = [
        {
            key: 'workbench',
            icon: <AppstoreOutlined />,
            label: '工作台',
            onClick: () => navigate('/units'),
        },
        {
            key: 'metrics',
            icon: <DatabaseOutlined />,
            label: '应用度量',
            onClick: () => navigate('/metrics'),
        },
        {
            key: 'admin',
            icon: <SettingOutlined />,
            label: '后台管理',
            children: [
                {
                    key: 'sa-scan-management',
                    icon: <ScanOutlined />,
                    label: 'SA扫描管理',
                    onClick: () => navigate('/sa/scan-management'),
                },
            ],
        },
    ];

    return (
        <Menu
            mode="inline"
            selectedKeys={[selectedKey]}
            defaultOpenKeys={openKeys}
            style={{ height: '100%', borderRight: 0 }}
            items={menuItems}
            inlineIndent={16}
        />
    );
};

export default Sidebar; 